package com.ztsoft.user.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ztsoft.user.common.exception.CustomException;
import com.ztsoft.user.common.pojo.PageEntity;
import com.ztsoft.user.pojo.entity.user.BaseEntity;
import com.ztsoft.user.utils.Result;
import com.ztsoft.user.pojo.enums.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

//新的BaseController
public class NewBaseController<T extends BaseEntity, K extends Serializable, S extends IService<T>> {

    @Autowired
    protected S baseService;

    //保存
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public Result<T> save(@RequestBody T t) {
        try {
            if (baseService.save(t)) {
                return Result.success();
            }
            return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    //根据id修改
    @PutMapping("/updateById")
    @ApiOperation(value = "修改")
    public Result<T> updateById(@RequestBody T t) {
        try {
            if (baseService.updateById(t)) {
                return Result.success();
            }
            return Result.fail();
        } catch (Exception e) {
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public Result<T> delete(@PathVariable("id") K k) {
        try {
            if (baseService.removeById(k)) {
                return Result.success();
            }
            return Result.fail();
        } catch (Exception e) {
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id查询一个")
    public Result<T> getById(@PathVariable("id") K k) {
        try {
            T t = baseService.getById(k);
            return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), t);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ResultCode.ERROR.getMsg(), ResultCode.ERROR.getCode());
        }
    }

    @GetMapping("/queryDataByPage")
    @ApiOperation(value = "带分页查询")
    public Result<PageEntity<T>> queryDataByPage(T t) {
        long size = t.getSize();
        long mount = t.getMount();

        Page<T> page = new Page<>(mount, size);
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>(t).orderBy(true, true, "create_time");

        baseService.page(page, queryWrapper);
        List<T> list = page.getRecords();
        int count = baseService.count(queryWrapper);

        PageEntity<T> pageEntity = new PageEntity<>(size, mount, count, list);
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), pageEntity);
    }

    //不分页，直接返回所有的
    @GetMapping("/getList")
    @ApiOperation(value = "不分页，直接返回所有的")
    public Result<List<T>> getList(T t) {
        List<T> list = baseService.list(new QueryWrapper<>(t));
        return Result.returnDataMsg(ResultCode.SUCCESS.getMsg(), ResultCode.SUCCESS.getCode(), list);
    }
}